package com.bd.rj.backstage.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.rj.backstage.service.IInterviewService;
import com.bd.rj.backstage.service.IPostService;
import com.bd.rj.backstage.service.IUserService;
import com.bd.rj.common.entity.OnlineInfo;
import com.bd.rj.common.entity.TeacherOnlineInfo;
import com.bd.rj.common.listener.OnlineUserListener;
import org.apache.catalina.core.StandardPipeline;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/homepage")
public class HomePageController {
    @Resource
    IUserService userService;
    @Resource
    IPostService postService;
    @Resource
    IInterviewService interviewService;

    /**
     * @desc 获取主页在线信息.
     * @author 白世俊
     * @date 2025/1/6
     */
    @RequestMapping(value = "getinfo", method = RequestMethod.GET)
    public Map<String, Object> getInfo() {
        //用户总数
        long userCount = userService.count();
        //在线用户数
        int onlineUsers = OnlineUserListener.online;
        //岗位总数
        long postCount = postService.count();
        //简历投递总数
        long interViewCount = interviewService.count();
        //本周岗位投递数
        Map<String, Object> weeklypost = postService.getWeeklyPost();
        //获取各类型岗位百分比
        List<Map<String, Object>> postTypePer = postService.getPostTypePer();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("createtime");
        queryWrapper.last("limit 8");
        List<Map<String, Object>> newestPost = postService.list(queryWrapper);
        Map<String, Object> info = new HashMap<>();
        //装载数据
        OnlineInfo onlineInfo = new OnlineInfo();
        onlineInfo.setPostCount(postCount);
        onlineInfo.setOnlineUsers(onlineUsers);
        onlineInfo.setUserCount(userCount);
        onlineInfo.setInterviewCount(interViewCount);
        onlineInfo.setOnlineUsers(onlineUsers);
        onlineInfo.setWeeklyPost(weeklypost);
        info.put("onlineInfo", onlineInfo);
        info.put("newestPost", newestPost);
        info.put("postTypePer", postTypePer);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "获取成功");
        map.put("data", info);
        return map;
    }

    /**
     * @desc 获取教师端首页信息
     * @author 白世俊
     * @date 2025/1/13
     */
    @RequestMapping(value = "/getTeacherInfo", method = RequestMethod.GET)
    public Map<String, Object> getTeacherInfo() {
        TeacherOnlineInfo teacherOnlineInfo = new TeacherOnlineInfo();
        long uid = StpUtil.getLoginIdAsLong();
        //接收到的简历数量
        int interviewRecordCount = interviewService.getInterviewCount(uid);
        //面试通过率
        double interviewPercentage = interviewService.getInterviewPercentage(uid);
        //简历投递趋势
        List<Map<String, Object>> interviewTrend = interviewService.getInterviewTrend(uid);
        //发布的岗位数量
        int postCount = postService.getPostCount(uid);
        List<Map<String, Object>> postPercentage = postService.getPostPercentage(uid);
        //最近发布的职位
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", uid);
//        queryWrapper.select("company", "postname", "address", "salary", "createtime", "status");
        queryWrapper.orderByDesc("createtime");
        queryWrapper.last("limit 8");
        List<Map<String, Object>> newestPost = postService.list(queryWrapper);
        //将数据装载到对象上
        Map<String, Object> info = new HashMap<>();
        info.put("teacherInfo", teacherOnlineInfo);
        info.put("postPercentage", postPercentage);
        info.put("interviewTrends", interviewTrend);
        info.put("newestPost", newestPost);
        teacherOnlineInfo.setInterviewCount(interviewRecordCount);
        teacherOnlineInfo.setInterviewPercentage(interviewPercentage);
        teacherOnlineInfo.setPostCount(postCount);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "获取成功");
        map.put("data", info);
        return map;
    }
}
